package server.messageContent;

import java.util.Date;

import common.SerializationManager;
import common.data.MonitorReplyTypeEnum;
import common.data.ResponseTypeEnum;
import common.interfaces.MessageContent;

public class MonitorReplyMsgContent implements MessageContent {
	private byte[] data;
	String responseString;
	public MonitorReplyMsgContent (MonitorReplyTypeEnum type){
		ResponseTypeEnum responseTypeEnum = ResponseTypeEnum.SUCCESSFUL;
		byte[] responseTypeStream = SerializationManager.getCurrent().fromResponseTypeEnum(responseTypeEnum);
		
		switch (type) {
		case OPEN:
			responseString = "You are monitoring the Server from " + new Date();
			break;
		case CLOSE:
			responseString = "Monitoring session is over.";
			break;
		}
		byte[] responseStream = SerializationManager.getCurrent()
				.fromString(responseString);
		byte[] responseStreamLength = SerializationManager.getCurrent()
				.fromInt(responseStream.length);
		
		byte[] responseObjectStream = SerializationManager.getCurrent().fromBoolean(true);
		byte[] responseObjectLengthStream = SerializationManager.getCurrent().fromInt(responseObjectStream.length);
		
		data = new byte[responseTypeStream.length
		                + responseStreamLength.length 
		                + responseStream.length
		                + responseObjectLengthStream.length
		                + responseObjectStream.length];
		int currentIndex = 0;
		
		System.arraycopy(responseTypeStream, 0, data, currentIndex, responseTypeStream.length);
		currentIndex += responseTypeStream.length;
		
		System.arraycopy(responseStreamLength, 0, data, currentIndex, responseStreamLength.length);
		currentIndex += responseStreamLength.length;
		
		System.arraycopy(responseStream, 0, data, currentIndex, responseStream.length);
		currentIndex += responseStream.length;
		
		System.arraycopy(responseObjectLengthStream, 0, data, currentIndex, responseObjectLengthStream.length);
		currentIndex += responseObjectLengthStream.length;
		
		System.arraycopy(responseObjectStream, 0, data, currentIndex, responseObjectStream.length);
		currentIndex += responseObjectStream.length;		
	}

	@Override
	public byte[] getByte() {
		// TODO Auto-generated method stub
		return this.data;
	}

	@Override
	public int getLength() {
		// TODO Auto-generated method stub
		return this.data.length;
	}

}
